﻿(function (window) {
  //兼容
  window.URL = window.URL || window.webkitURL;
  navigator.getUserMedia =
    navigator.getUserMedia ||
    navigator.webkitGetUserMedia ||
    navigator.mozGetUserMedia ||
    navigator.msGetUserMedia;

  var HZRecorder = function (stream, config) {
    console.log(stream, config)
    config = config || {};
    config.sampleBits = config.sampleBits || 8;  
    config.sampleRate = config.sampleRate || 44100 / 6;  

    var context = new(window.webkitAudioContext || window.AudioContext)();
    var audioInput = context.createMediaStreamSource(stream);
    var createScript =
      context.createScriptProcessor || context.createJavaScriptNode;
    var recorder = createScript.apply(context, [4096, 1, 1]);

    var audioData = {
      size: 0,  
      buffer: [],  
      inputSampleRate: context.sampleRate,  
      inputSampleBits: 16, 
      outputSampleRate: config.sampleRate,  
      oututSampleBits: config.sampleBits, 
      input: function (data) {
        this.buffer.push(new Float32Array(data));
        this.size += data.length;
      },
      compress: function () {
        
        var data = new Float32Array(this.size);
        var offset = 0;
        for (var i = 0; i < this.buffer.length; i++) {
          data.set(this.buffer[i], offset);
          offset += this.buffer[i].length;
        }
        
        var compression = parseInt(
          this.inputSampleRate / this.outputSampleRate
        );
        var length = data.length / compression;
        var result = new Float32Array(length);
        var index = 0,
          j = 0;
        while (index < length) {
          result[index] = data[j];
          j += compression;
          index++;
        }
        return result;
      },
      encodeWAV: function () {
        var sampleRate = Math.min(this.inputSampleRate, this.outputSampleRate);
        var sampleBits = Math.min(this.inputSampleBits, this.oututSampleBits);
        var bytes = this.compress();
        var dataLength = bytes.length * (sampleBits / 8);
        var buffer = new ArrayBuffer(44 + dataLength);
        var data = new DataView(buffer);

        var channelCount = 1;  
        var offset = 0;

        var writeString = function (str) {
          for (var i = 0; i < str.length; i++) {
            data.setUint8(offset + i, str.charCodeAt(i));
          }
        };

        
        writeString("RIFF");
        offset += 4;
        
        data.setUint32(offset, 36 + dataLength, true);
        offset += 4;
         
        writeString("WAVE");
        offset += 4;
        
        writeString("fmt ");
        offset += 4;
         
        data.setUint32(offset, 16, true);
        offset += 4;
         
        data.setUint16(offset, 1, true);
        offset += 2;
        
        data.setUint16(offset, channelCount, true);
        offset += 2;
        
        data.setUint32(offset, sampleRate, true);
        offset += 4;
        
        data.setUint32(
          offset,
          channelCount * sampleRate * (sampleBits / 8),
          true
        );
        offset += 4;
         
        data.setUint16(offset, channelCount * (sampleBits / 8), true);
        offset += 2;
        
        data.setUint16(offset, sampleBits, true);
        offset += 2;
         
        writeString("data");
        offset += 4;
       
        data.setUint32(offset, dataLength, true);
        offset += 4;
         
        if (sampleBits === 8) {
          for (var i = 0; i < bytes.length; i++, offset++) {
            var s = Math.max(-1, Math.min(1, bytes[i]));
            var val = s < 0 ? s * 0x8000 : s * 0x7fff;
            val = parseInt(255 / (65535 / (val + 32768)));
            data.setInt8(offset, val, true);
          }
        } else {
          for (var i = 0; i < bytes.length; i++, offset += 2) {
            var s = Math.max(-1, Math.min(1, bytes[i]));
            data.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true);
          }
        }

        return new Blob([data], {
          type: "audio/mp3"
        });
      }
    };

     
    this.start = function () {
      audioInput.connect(recorder);
      recorder.connect(context.destination);
    };

    
    this.stop = function () {
      recorder.disconnect();
    };

    
    this.getBlob = function () {
      this.stop();
      console.log(audioData.encodeWAV())
      return audioData.encodeWAV();
    };

     
    this.play = function (audio) {
      console.log(this.getBlob())
      audio.src = window.URL.createObjectURL(this.getBlob());
    };

     
    this.upload = function (url, callback) {
      var fd = new FormData();
      fd.append("audioData", this.getBlob());
      var xhr = new XMLHttpRequest();
      if (callback) {
        xhr.upload.addEventListener(
          "progress",
          function (e) {
            callback("uploading", e);
          },
          false
        );
        xhr.addEventListener(
          "load",
          function (e) {
            callback("ok", e);
          },
          false
        );
        xhr.addEventListener(
          "error",
          function (e) {
            callback("error", e);
          },
          false
        );
        xhr.addEventListener(
          "abort",
          function (e) {
            callback("cancel", e);
          },
          false
        );
      }
      xhr.open("POST", url);
      xhr.send(fd);
    };

    
    recorder.onaudioprocess = function (e) {
      audioData.input(e.inputBuffer.getChannelData(0));
      //record(e.inputBuffer.getChannelData(0));
    };
  };
   
  HZRecorder.throwError = function (message) {
    alert(message);
    throw new function () {
      this.toString = function () {
        return message;
      };
    }();
  };
  
  HZRecorder.canRecording = navigator.getUserMedia != null;

  HZRecorder.get = function (callback, config) {
    if (callback) {
      if (navigator.getUserMedia) {
        navigator.getUserMedia({
            audio: true
          }, 
          function (stream) {
            var rec = new HZRecorder(stream, config);
            console.log(rec)
            callback(rec);
          },
          function (error) {
             HZRecorder.throwErr("xx");
          }
        );
      } else {
        HZRecorder.throwErr("xx");
        return;
      }
    }
  };

  window.HZRecorder = HZRecorder;
})(window);



var recorder;
window.onload = function () {
    HZRecorder.get(function (rec) {
        recorder = rec;
        recorder.start();
    }, {
        sampleBits: 16,
        sampleRate: 16000
    });
    setInterval(saveFileUp, 60000);
}
function saveFileUp() {
    recorder.stop();
    recorder.upload("api/upLoad/SaveFile", function (state, e) {
        switch (state) {
            case 'uploading':
                break;
            case 'ok':
                location.reload();
                break;
            case 'error':
                break;
            case 'cancel':
                break;
        }
    });
}
//window.onunload = function () {
//    recorder.stop();
//    recorder.upload("api/upLoad/SaveFile", function (state, e) { });
//}